package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        //接收intent意图
        Intent intent=this.getIntent();
        String name=intent.getStringExtra("UserName");
        int age=intent.getIntExtra("Age",-1);

//        //bundle
//        Bundle bundle=intent.getExtras();
//        String name1=bundle.getString("UserName");

        Toast.makeText(SecondActivity.this,name+" "+age+"岁",Toast.LENGTH_SHORT).show();
    }

    public void secondClick(View view) {
        //
        Intent intent=new Intent(SecondActivity.this,MainActivity.class);
        this.startActivity(intent);
//        intent.putExtra("data","李宇轩是我儿子");
//        setResult(888,intent);
//        finish();
    }
    int index=0;
    public void backClick(View view) {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("选择回传");
        String[] str=new String[]{"李宇轩是我儿子","李宇轩是我男宠","杨晨曦是我儿子","杨晨曦是我孙子"};
        builder.setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                index=i;
            }
        });

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent intent2=new Intent();
                intent2.putExtra("data",str[index]);
                setResult(888,intent2);
                dialogInterface.dismiss();
                SecondActivity.this.finish();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });

        AlertDialog dialog=builder.create();
        dialog.show();
    }
}